/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System.Text;
using OpenServers.Net.Common.Listener;
using OpenServers.Net.Common.Socket;
using OpenServers.Net.Common.State;
using OpenServers.Net.Common.User;
using OpenServers.Net.HttpServer.Request;
using OpenServers.Net.HttpServer.Response;

namespace OpenServers.Net.HttpServer.State {
    /// <summary>
    /// State object containing information need to process an HTTP request.
    /// </summary>
    public class HttpState : AbstractState {
        /// <summary>
        /// Enumeration of states while reading a request.
        /// </summary>
        public enum RequestStateEnum {
            /// <summary>
            /// Currently reading the header of the request.
            /// </summary>
            Header,

            /// <summary>
            /// Currently reading the body of the request.
            /// </summary>
            Body,

            /// <summary>
            /// Done reading the body. ready to send response.
            /// </summary>
            BodyComplete
        }

        private readonly StringBuilder _requestBuffer = new StringBuilder();
        private HttpRequest _request;
        private RequestStateEnum _requestState;
        private HttpResponse _response;
        private IUser _user;

        /// <summary>
        /// constructor.
        /// </summary>
        public HttpState(IListener listener, ISocket clientSocket)
            : base(listener, clientSocket) {
        }

        /// <summary>
        /// The request buffer. Used for reading and parsing the header of the request.
        /// </summary>
        public StringBuilder RequestBuffer {
            get { return _requestBuffer; }
        }

        /// <summary>
        /// The current state of reading the request.
        /// </summary>
        public RequestStateEnum RequestState {
            get { return _requestState; }
            set { _requestState = value; }
        }

        /// <summary>
        /// The request object.
        /// </summary>
        public HttpRequest Request {
            get { return _request; }
            set { _request = value; }
        }

        /// <summary>
        /// The response object.
        /// </summary>
        public HttpResponse Response {
            get { return _response; }
            set { _response = value; }
        }

        /// <summary>
        /// The current user.
        /// </summary>
        public IUser User {
            get { return _user; }
            set { _user = value; }
        }
    }
}
